var touchIdentifierForSlider = {};
var valueForSlider = {};
var initialTouchPageX = {};
var initialSliderControlLeft = {};
var avgValues;

function initializeSliders()
{
	// if this isn't the most recently viewed class, we need to set the sliders to the average value
	if(previouslyViewedClassID != currentClass.uniqueCourseNumber)
	{
		$.getJSON('php/metrics.php',{action:'get_metrics', uniqueCourseNumber:currentClass.uniqueCourseNumber},
			function(result)
			{
				// update the sliders and average indicators to the correct css positions
				setClassSliders(result);
				setSliders(result);
			}
		);
	}
}

function sliderTouchStarted(event)
{
	if (event.changedTouches.length != 0)
	{
		event.preventDefault();
		
		// determine which slider we're dealing with
		var sliderIdentifier = $(event.target).parent('.sliderBar').attr('id');	// get the ID for the slider
		
		// set a new touch identifier for this slider
		touchIdentifierForSlider[sliderIdentifier] = event.changedTouches[0].identifier;
		
		// set the initial values
		initialTouchPageX[sliderIdentifier] = event.changedTouches[0].pageX;
		initialSliderControlLeft[sliderIdentifier] = parseFloat($(event.target).css('left'));
		
		// calculate and display the new left offset for the slider control
		var sliderControlPosition = calculateSliderControlPosition(event,sliderIdentifier);
		$(event.target).css('left',sliderControlPosition);
		
		// store the new value locally
		valueForSlider[sliderIdentifier] = calculateSliderValue(event,sliderIdentifier);
	}
}

function sliderTouchMoved(event)
{
	if (event.changedTouches.length != 0)
	{
		// determine which slider we're dealing with
		var sliderIdentifier = $(event.target).parent('.sliderBar').attr('id');	// get the ID for the slider
		
		// only continue if this touch is the active one in the slider
		if ( touchIdentifierForSlider[sliderIdentifier] == event.changedTouches[0].identifier )
		{
			event.preventDefault();
			
			// calculate and display the new left offset for the slider control
			var sliderControlPosition = calculateSliderControlPosition(event,sliderIdentifier);
			$(event.target).css('left',sliderControlPosition);
			
			// store the new value locally
			valueForSlider[sliderIdentifier] = calculateSliderValue(event,sliderIdentifier);
		}
	}
}

function sliderTouchEnded(event)
{
	// submit the new value to the server
	var sliderIdentifier = $(event.target).parent('.sliderBar').attr('id');
	
	$.getJSON('php/metrics.php',{
		action:'submit_metric',
		uniqueCourseNumber: currentClass.uniqueCourseNumber,
		metricName:sliderIdentifier,
		metricValue:valueForSlider[sliderIdentifier]},
		function(result)
		{
			if(result.success != 'true')
			{
				console.error('Could not submit metric update');
			}
		}
	);
}

function calculateSliderControlPosition(event,sliderIdentifier)
{
	var sliderBar = $(event.target).parent('.sliderBar');
	var sliderControl = $('.sliderControl',sliderBar);
	var maxLeft = sliderBar.width() - sliderControl.width(); // calculate the max left offset
	
	var newLeft = initialSliderControlLeft[sliderIdentifier] + calculateTouchDelta(event,sliderIdentifier);
	
	return Math.max(Math.min(newLeft, maxLeft), 0);
}

function calculateTouchDelta(event,sliderIdentifier)
{
	var delta = event.changedTouches[0].pageX - initialTouchPageX[sliderIdentifier];
	
	return delta;
}

function calculateSliderValue(event,sliderIdentifier)
{
	var sliderBar = $(event.target).parent('.sliderBar');
	var sliderControl = $('.sliderControl',sliderBar);
	var maxLeft = sliderBar.width() - sliderControl.width(); // calculate the max left offset
	
	var newLeft = initialSliderControlLeft[sliderIdentifier] + calculateTouchDelta(event,sliderIdentifier);
	
	return Math.max(Math.min(newLeft, maxLeft), 0) / maxLeft;
}

function setClassSliders(newValues)
{
	avgValues = newValues;	// cache the new values
	
	$('.sliderBar').each(
		function(i)
		{
			var sliderBar = $(this);
			var classControl = $('.sliderControlForClass',sliderBar);
			var maxLeft = sliderBar.width() - classControl.width(); // calculate the max left offset
			
			// set the new value
			var sliderValue = newValues[sliderBar.attr('id')];
			
			classControl.css('left',sliderValue * maxLeft);
		}
	);
}

function setSliders(newValues)
{
	$('.sliderBar').each(
		function(i)
		{
			var sliderBar = $(this);
			var sliderControl = $('.sliderControl',sliderBar);
			var maxLeft = sliderBar.width() - sliderControl.width(); // calculate the max left offset
			
			// set the new value
			var sliderValue = newValues[sliderBar.attr('id')];
			
			sliderControl.css('left',sliderValue * maxLeft);
		}
	);
}

function updateSliderBarBackground(event, info)
{
	if(info.orientation == 'landscape')
	{
		// update the background of the slider bar
		$('.sliderBar').css('background-image',"url('./images/sliderBarBackground438.png')");
	}
	else if(info.orientation == 'profile')
	{
		// update the background of the slider bar
		$('.sliderBar').css('background-image',"url('./images/sliderBarBackground278.png')");
	}
	
	// update the left offset for the slider controls
	$('.sliderControl').each(refreshPosition);
	$('.sliderControlForClass').each(refreshPosition);
}

function refreshPosition()
{
	var control = $(this);
	var sliderBar = control.parent('.sliderBar');
	
	var maxLeft = sliderBar.width() - control.width(); // calculate the max left offset
	
	// determine which type of control this is
	var controlType = control.attr('class');
	
	// set the new value
	if(controlType == 'sliderControlForClass')
		var sliderValue = avgValues[sliderBar.attr('id')];
	else
		var sliderValue = valueForSlider[sliderBar.attr('id')];
	
	control.css('left',sliderValue * maxLeft);
}